package com.dxq.basic_ui_library;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.google.gson.Gson;

import java.util.List;

/**
 * @author zsh
 * @date 2020/2/19.
 * 功能：自定义下拉框
 */

public class SpinnerView<T> extends FrameLayout {

    private Context mContext;
    private View mView;
    private LinearLayout llView;
    private TextView mText;
    private ImageView mImageView;
    private SpinnerPopWindow mSpinnerPopWindow;

    private List<T> spinnerList;
    private int viewBg;
    private int height;
    private int textSize;
    private int textColor;
    private int popHeight;
    private int showIcon;

    private boolean isShow = false;
    private RotateAnimation animation;//旋转特效 刷新中箭头翻转 向下变向上
    private RotateAnimation reverseAnimation;

    public SpinnerView(Context context) {
        super(context);
        init(context, null);
    }

    public SpinnerView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public SpinnerView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public SpinnerView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context, attrs);
    }

    //初始化
    private void init(Context context, AttributeSet attrs) {
        mContext = context;
        if (attrs == null) return;
        //获取自定义属性
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.SpinnerView);
        //输入框背景
        viewBg = typedArray.getResourceId(R.styleable.SpinnerView_background, R.drawable.input_bg);
        //输入框高度
        height = typedArray.getDimensionPixelOffset(R.styleable.SpinnerView_height, 40);
        //字体大小
        textSize  = typedArray.getDimensionPixelSize(R.styleable.SpinnerView_text_size, 20);
        //字体颜色
        textColor = typedArray.getColor(R.styleable.SpinnerView_text_color, Color.BLACK);
        //下拉框高度
        popHeight = typedArray.getDimensionPixelSize(R.styleable.SpinnerView_pop_height, 100);
        //展示按钮
        showIcon = typedArray.getResourceId(R.styleable.SpinnerView_image_icon, R.drawable.up);

        typedArray.recycle();

        mView = LayoutInflater.from(context).inflate(R.layout.spinner_view, this, false);
        llView = (LinearLayout) mView.findViewById(R.id.ll_view);
        mText = (TextView) mView.findViewById(R.id.tv_msg);
        mImageView = (ImageView) mView.findViewById(R.id.image);
        addView(mView);

        //布局关联属性
        llView.setBackgroundResource(viewBg);
        //llView.setMinimumHeight(height);
        mText.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
        mText.setTextColor(textColor);
        mImageView.setImageResource(showIcon);

        //显示PopupWindow
        llView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mSpinnerPopWindow.setWidth(llView.getWidth());
                mSpinnerPopWindow.setHeight(SizeUtils.dp2px(popHeight));
                mSpinnerPopWindow.showAsDropDown(llView);
                setTextImage(showIcon);
            }
        });

        animation = new RotateAnimation(0, -180,
                RotateAnimation.RELATIVE_TO_SELF, 0.5f,
                RotateAnimation.RELATIVE_TO_SELF, 0.5f);
        animation.setInterpolator(new LinearInterpolator());
        animation.setDuration(200);
        animation.setFillAfter(true);//特效animation设置

        reverseAnimation = new RotateAnimation(-180, 0,
                RotateAnimation.RELATIVE_TO_SELF, 0.5f,
                RotateAnimation.RELATIVE_TO_SELF, 0.5f);
        reverseAnimation.setInterpolator(new LinearInterpolator());
        reverseAnimation.setDuration(200);
        reverseAnimation.setFillAfter(true);//特效reverseAnimation设置

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = measureWidth(widthMeasureSpec);
        int height = measureHeight(heightMeasureSpec);
        //设置宽高
        //setMeasuredDimension(width, height);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    private int measureWidth(int measureSpec) {
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);
        //wrap_content
        if (specMode == MeasureSpec.AT_MOST){
        }
        //fill_parent或者精确值
        else if (specMode == MeasureSpec.EXACTLY){
        }
        return specSize;
    }
    //根据xml的设定获取高度
    private int measureHeight(int measureSpec) {
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);
        //wrap_content
        if (specMode == MeasureSpec.AT_MOST){
            //specSize =
        }
        //fill_parent或者精确值
        else if (specMode == MeasureSpec.EXACTLY){
        }
        return specSize;
    }

    public void initSpinnerPopWindow(List<T> spinnerList) {
        this.spinnerList = spinnerList;
        mSpinnerPopWindow = new SpinnerPopWindow<T>(mContext, spinnerList, new SpinnerItemClickListener());
        //监听popupwindow取消
        mSpinnerPopWindow.setOnDismissListener(dismissListener);
    }

    //设置图片
    private void setTextImage(int resId) {
//        Drawable drawable = getResources().getDrawable(resId);
//        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());// 必须设置图片大小，否则不显示
//        mText.setCompoundDrawables(null, null, drawable, null);
        mImageView.clearAnimation();
        if (isShow) {
            //已展开，要关闭
            mImageView.startAnimation(reverseAnimation);
        } else {
            //已关闭，要展开
            mImageView.startAnimation(animation);
        }
        isShow = !isShow;
    }

    //设置内容
    public void setTextContent(String text) {
        mText.setText(text);
    }

    /**
     * popupwindow显示的ListView的item点击事件
     */
    class SpinnerItemClickListener implements AdapterView.OnItemClickListener {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            mSpinnerPopWindow.dismiss();
            T t = spinnerList.get(position);
            setTextContent(t.toString());
            if (null != spinnerItemListener)
                spinnerItemListener.itemClick(t);
        }
    }

    /**
     * 监听popupwindow取消
     */
    private PopupWindow.OnDismissListener dismissListener = new PopupWindow.OnDismissListener() {
        @Override
        public void onDismiss() {
            setTextImage(showIcon);
        }
    };

    public interface SpinnerItemListener<T> {
        void itemClick(T t);
    }

    public SpinnerItemListener spinnerItemListener;

    public void setSpinnerItemListener(SpinnerItemListener spinnerItemListener) {
        this.spinnerItemListener = spinnerItemListener;
    }

}
